package com.admin.service;

import com.admin.dao.SupplierDao;
import com.admin.entity.Supplier;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.admin.sys.utils.admin.StringUtils;
import com.admin.sys.utils.admin.ObjectUtils;
import com.admin.sys.base.module.extend.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.List;

//供应商信息接口类
@Service
public class SupplierService extends BaseService {

    @Autowired
    private SupplierDao supplierDao;

    //【供应商信息】设置查询条件
    private LambdaQueryWrapper<Supplier> getSupplierQueryCondition(Supplier supplier) {
        LambdaQueryWrapper<Supplier> lambdaQuery = this.getBaseQueryCondition(supplier);
        //根据创建时间排序
        lambdaQuery.orderByDesc(Supplier::getCreateDate);
        if (ObjectUtils.isNotNull(supplier)) {
            //【主键id】精确查询
            lambdaQuery.eq(StringUtils.isNotEmpty(supplier.getId()), Supplier::getId, supplier.getId());
            //【供应商名称】模糊查询
            lambdaQuery.like(StringUtils.isNotEmpty(supplier.getName()), Supplier::getName, supplier.getName());
            //【联系人】模糊查询
            lambdaQuery.like(StringUtils.isNotEmpty(supplier.getUserName()), Supplier::getUserName, supplier.getUserName());
            //【联系电话】模糊查询
            lambdaQuery.like(StringUtils.isNotEmpty(supplier.getPhone()), Supplier::getPhone, supplier.getPhone());
            //【邮箱】模糊查询
            lambdaQuery.like(StringUtils.isNotEmpty(supplier.getEmail()), Supplier::getEmail, supplier.getEmail());
        }
        return lambdaQuery;
    }

    //【供应商信息】分页查询
    public IPage<Supplier> getPage(Page<Supplier> page, Supplier supplier) {
        LambdaQueryWrapper<Supplier> lambdaQuery = getSupplierQueryCondition(supplier);
        return supplierDao.selectPage(page, lambdaQuery);
    }
    
    //【供应商信息】查询列表
    public List<Supplier> getList(Supplier supplier) {
        LambdaQueryWrapper<Supplier> lambdaQuery = getSupplierQueryCondition(supplier);
        return supplierDao.selectList(lambdaQuery);
    }
    
    //【供应商信息】根据id查询
    public Supplier get(String id) {
        return supplierDao.selectById(id);
    }

    //【供应商信息】根据对象查询
    public Supplier get(Supplier supplier) {
    LambdaQueryWrapper<Supplier> lambdaQuery = getSupplierQueryCondition(supplier);
        return supplierDao.selectOne(lambdaQuery);
    }
    
    //【供应商信息】新增
    public int insert(Supplier supplier) {
        this.preInsert(supplier);
        return supplierDao.insert(supplier);
    }
    
    //【供应商信息】修改
    public int update(Supplier supplier) {
        this.preUpdate(supplier);
        return supplierDao.updateById(supplier);
    }
    
    //【供应商信息】删除
    public int delete(String id) {
        return supplierDao.deleteById(id);
    }

    //【供应商信息】批量删除
    public int delAll(String[] ids) {
        return supplierDao.deleteBatchIds(Arrays.asList(ids));
    }

}